<?php
namespace app\shop\controller;

use app\common\model\shop\PlatformConfigModel;
use app\common\model\shop\PlatformModel;
use think\admin\Controller;
use think\admin\helper\QueryHelper;


/**
 * 商城平台配置
 * @package app\admin\controller
 * @author wangqinghui
 * @DateTime 2024-09-11 13:58:44
 */
class Platform extends Controller
{
    /**
     * 平台列表
     * @auth true
     * @menu true
     * @author Lihongqi
     * @DateTime 2024-09-11 14:56:28
     * @return void
     */
    public function index ()
    {
        PlatformModel::mQuery()->layTable(function () {
            $this->title = '平台列表';

        }, function (QueryHelper $query) {

            // 数据列表搜索过滤
            $query->like('name')->equal('status')->dateBetween('create_at');
            //$query->order('sort desc, id asc');
        });
    }

    protected function _index_page_filter (&$data)
    {

        $type_arr = PlatformConfigModel::mk()->where('account_id', $this->account_id)->column('type');

        foreach ($data as &$item) {

            $item['is_set'] = in_array($item['type'], $type_arr) ? 1 : 0;
        }
    }

    public function config ()
    {
        $type = $this->request->get('type');

        $config = PlatformConfigModel::mk()->where('type', $type)->where('account_id', $this->account_id)->find();

        if ( $this->request->isPost() ) {

            $data = $this->request->post();

            $data_json = json_encode($data);

            if (!$config) {

                $config =  new PlatformConfigModel();

                $config->type = $type;

                $config->account_id = $this->account_id;

            }

            $config->config = $data_json;

            $config->save();

        }

        $platform = PlatformModel::where('type', $type)->find();

        $this->config = $config;

        $this->fetch($platform->template);

    }



}